package com.jingyanzi.channel.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.channel.dao.ChanneltypeMapper;
import com.jingyanzi.channel.domain.ChannelTypeDO;
import com.jingyanzi.channel.entity.Channeltype;
import com.jingyanzi.channel.entity.ChanneltypeExample;
import com.jingyanzi.channel.entity.ChanneltypeExample.Criteria;
import com.jingyanzi.channel.query.ChannelTypeQueryDO;
import com.jingyanzi.channel.result.ResultDO;
import com.jingyanzi.channel.result.ResultSupport;
import com.jingyanzi.channel.service.IChannelTypeService;
import com.jingyanzi.channel.util.BeanUtilsExtends;


@Service("channeltypeservice")
public class ChannelTypeServiceImpl extends BaseServiceImpl implements IChannelTypeService {

	@Autowired
	private ChanneltypeMapper channeltypeMapper;

	@Override
	public ResultDO add(ChannelTypeDO channelTypeDO) {
		Channeltype channeltype = new Channeltype();

		ResultSupport result = BeanUtilsExtends.copy(channeltype, channelTypeDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		channeltype.setCreatetime(Calendar.getInstance().getTime());
		channeltype.setIsDelete(1);
		int r = -1;

		try {
			r = channeltypeMapper.insert(channeltype);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加渠道分类失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加渠道分类失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channeltype.getId());
		return result;
	}
	
	public ResultDO checkRepeat(ChannelTypeDO domain) {
		
		ResultSupport result = new ResultSupport();
		
		ChanneltypeExample example = new ChanneltypeExample();

		Criteria criteria = example.createCriteria();
		
		if(isNumberNotEmpty(domain.getId())) {
			criteria.andIdNotEqualTo(domain.getId());
			if(StringUtils.isNotEmpty(domain.getChannelName())) {
				criteria.andChannelNameEqualTo(domain.getChannelName());
			}
			if(StringUtils.isNotEmpty(domain.getCode())) {
				criteria.andCodeEqualTo(domain.getCode());
			}
		} else {
			if(StringUtils.isNotEmpty(domain.getChannelName())) {
				criteria.andChannelNameEqualTo(domain.getChannelName());
			}
			if(StringUtils.isNotEmpty(domain.getCode())) {
				criteria.andCodeEqualTo(domain.getCode());
			}
		}
		if(StringUtils.isNotEmpty(domain.getCode()) || StringUtils.isNotEmpty(domain.getChannelName())) {
			int count = 0;
			
			try {
				count = channeltypeMapper.countByExample(example);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "分类查询失败");
				result.setSuccess(false);
				return result;
			}
			
			if(count > 0) {
				result.setModel(ResultDO.FIRST_MODEL_KEY, count);
				return result;
			}
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, 0);
		
		return result;
	}

	@Override
	public ResultDO modifi(ChannelTypeDO channelTypeDO) {
		Channeltype channeltype = new Channeltype();

		ResultSupport result = BeanUtilsExtends.copy(channeltype, channelTypeDO);
		if (!result.isSuccess()) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		channeltype.setModifitime(Calendar.getInstance().getTime());
		int r = -1;

		try {
			r = channeltypeMapper.updateByPrimaryKeySelective(channeltype);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改渠道分类失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改渠道分类失败");
			result.setSuccess(false);
			return result;
		}

		result.setModel(ResultDO.FIRST_MODEL_KEY, channeltype.getId());
		return result;
	}

	@Override
	public ResultDO remove(Integer[] ids) {
		Channeltype channeltype = new Channeltype();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < ids.length; i++) {
			channeltype.setId(ids[i]);
			channeltype.setIsDelete(-1);
			int r = -1;

			try {
				r = channeltypeMapper.updateByPrimaryKeySelective(channeltype);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除渠道分类失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除渠道分类失败");
				result.setSuccess(false);
				return result;
			}
		}
		result.setModel(ResultDO.FIRST_MODEL_KEY, channeltype.getId());
		return result;
	}

	@Override
	public ResultDO get(Integer id) {
		Channeltype channeltype = new Channeltype();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;

		try {
			channeltype = channeltypeMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道分类失败");
			result.setSuccess(false);
			return result;
		}

		if (channeltype == null) {
			result.setErrorCode(ResultDO.CHANNELTYPE_GET_ERROR);
			result.setErrorMsg("不存在该渠道分类");
			result.setSuccess(false);
			return result;
		}
		ChannelTypeDO channelTypeDO = getChannelTypeDO(channeltype);
		result.setModel(ResultDO.FIRST_MODEL_KEY, channelTypeDO);
		return result;
	}

	public ResultDO getChanneltypeList(Integer id) {

		ResultSupport result = new ResultSupport();

		result.setModel(ResultSupport.FIRST_MODEL_KEY, list(id));

		return result;

	}

	@Override
	public ResultDO query(ChannelTypeQueryDO channelTypeQueryDO) {
		ResultSupport result = new ResultSupport();

		List<Channeltype> list = null;

		ChanneltypeExample example = new ChanneltypeExample();
		Criteria createCriteria = example.createCriteria();
		if (channelTypeQueryDO.getFatherTypeId() != null) {
			createCriteria.andFatherTypeIdEqualTo(channelTypeQueryDO.getFatherTypeId());
		}
		if (StringUtils.isNotEmpty(channelTypeQueryDO.getName())) {
			createCriteria.andChannelNameLike("%" + channelTypeQueryDO.getName() + "%");
		}

		createCriteria.andIsDeleteEqualTo(1);

		List<ChannelTypeDO> channelTypeDOs = null;

		if (channelTypeQueryDO.isPage()) {
			example.setLimitByPage(channelTypeQueryDO.getStart() + "," + channelTypeQueryDO.getPageRows());
			long count = channeltypeMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = channeltypeMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道分类失败");
			result.setSuccess(false);
			return result;
		}
		channelTypeDOs = getChannelTypeDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, channelTypeDOs);

		return result;
	}

	public List<ChannelTypeDO> list(Integer fatherTypeId) {

		List<Channeltype> list = null;
		ChanneltypeExample example = new ChanneltypeExample();

		example.createCriteria().andFatherTypeIdEqualTo(fatherTypeId).andIsDeleteEqualTo(1);

		try {
			list = channeltypeMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
		}

		List<ChannelTypeDO> orgDOList = new ArrayList<ChannelTypeDO>();

		if (list == null || list.size() == 0) {
			return orgDOList;
		}

		for (Channeltype channeltype : list) {
			ChannelTypeDO channelTypeDO = getChannelTypeDO(channeltype);
			channelTypeDO.setChildren(list(channeltype.getId()));
			orgDOList.add(channelTypeDO);
		}

		return orgDOList;
	}

	@Override
	public ResultDO isDel(Integer id) {
		ResultSupport result = new ResultSupport();
		int i = 0;
		List<Channeltype> list = null;
		ChannelTypeDO channelTypeDO = new ChannelTypeDO();
		ChanneltypeExample example = new ChanneltypeExample();
		Criteria createCriteria = example.createCriteria();

		createCriteria.andIsDeleteEqualTo(1).andFatherTypeIdEqualTo(id);
		try {
			list = channeltypeMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道分类失败");
			result.setSuccess(false);
			return result;
		}
		if (list.size() != 0) {
			i = 1;
		}else {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取渠道分类失败");
			result.setSuccess(false);
			return result;
		}
		channelTypeDO.setIsHave(i);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, channelTypeDO);

		return result;
	}
}
